package capricornus.components.sound
{
	import capricornus.utils.roughtime.RoughTimer;
	import capricornus.utils.roughtime.RoughTimerEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	/**
	 * 对于播放一次的，如果在4s内未开始播放则停止播放
	 * @author kael
	 * 
	 */	
	internal class MySound
	{
		private var _url:String;
		private var _time:int;
		private var _delayTimeOnComplete:uint;
		
		private var _isCheckLoad:Boolean;
		private var _isOpen:Boolean = true;
		private var _timer:RoughTimer;
		
		private var _isError:Boolean = false;
		private var _sound:Sound;
		private var _arrSoundChannel:Array;
		
		public function MySound( url:String, delayTimeOnComplete:uint, isCheckLoad:Boolean=true )
		{
			_url = url;
			_delayTimeOnComplete = delayTimeOnComplete;
			_isCheckLoad = isCheckLoad;
			init();
		}
		
		private function init():void
		{
			_arrSoundChannel = new Array();
			
			_sound = new Sound();
			try
			{
				_sound.load( new URLRequest( _url ) );
			}
			catch( e:Error )
			{
				_sound = null;
			}
			if( _sound != null )
			{
				_sound.addEventListener( Event.OPEN, onOpen, false, 0, true );
				_sound.addEventListener( IOErrorEvent.IO_ERROR, onError, false, 0, true );
				_sound.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true );
				
				_timer = new RoughTimer( 4*1000, 1 );
				_timer.addEventListener( RoughTimerEvent.TIMER, onTimerOut, false, 0, true );
			}
		}
		
		/**
		 * 
		 * @param time
		 * @param isUniqueness 是否唯一声道播放
		 * 
		 */		
		public function play( time:int, isUniqueness:Boolean=true ):void
		{
			_time = time;
			_isUniqueness = isUniqueness;
			next();
		}
		
		private var _isUniqueness:Boolean = false;
		private var _uniqueChannel:SoundChannel;
		private function next():void
		{
			if( _isUniqueness && _uniqueChannel )
			{
				return;
			}
			if( !_isError && _sound != null && (_time > 0 || -1==_time))
			{
				if( _time != -1 ) _time--;
				_uniqueChannel = _sound.play();
				if( _uniqueChannel != null )
				{
					_uniqueChannel.soundTransform = new SoundTransform( 0.8/*SoundManager.instance.soundVolume*/ );
					_uniqueChannel.addEventListener( Event.SOUND_COMPLETE, onComplete, false, 0, true );
					
					if( !_isOpen && _isCheckLoad )
					{
						_arrSoundChannel.push( _uniqueChannel );
						_timer.start();
					}
				}
			}
		}
		
		public function stop():void
		{
			for( var i:int=0; i<_arrSoundChannel.length; i++ )
			{
				SoundChannel(_arrSoundChannel[i]).stop();
			}
			if( _uniqueChannel != null )
			{
				_uniqueChannel.stop();
				_uniqueChannel.removeEventListener( Event.SOUND_COMPLETE, onComplete );
				_uniqueChannel = null;
			}
		}
		
		private function onTimerOut( e:RoughTimerEvent ):void
		{
			stop();
		} 
		
		private function onOpen( e:Event ):void
		{
			_isOpen = true;
			_timer.stop();
		}
		
		private function onComplete( e:Event ):void
		{
			var sc:SoundChannel = e.currentTarget as SoundChannel;
			sc.removeEventListener( Event.SOUND_COMPLETE, onComplete );
			_uniqueChannel = null;
			
			next();
		}
		
		private function onError( e:Event ):void
		{
			_isError = true;
		}
	}
}













